home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / stringsearch / bmsource / slfC.fwd.inc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-06  |  2.2 KB  |  115 lines  |  [TEXT/MPS ]

  1. /*
  2.     search routine generated by gen.
  3.     skip=slfC, match=fwd, shift=inc
  4. */
  5. #include    "freq.h"
  6. /*
  7.  * The authors of this software are Andrew Hume and Daniel Sunday.
  8.  * 
  9.  * Copyright (c) 1991 by AT&T and Daniel Sunday.
  10.  * 
  11.  * Permission to use, copy, modify, and distribute this software for any
  12.  * purpose without fee is hereby granted, provided that this entire notice
  13.  * is included in all copies of any software which is or includes a copy
  14.  * or modification of this software and in all copies of the supporting
  15.  * documentation for such software.
  16.  * 
  17.  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY
  19.  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  20.  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  21.  */
  22.  
  23. #ifndef    CHARTYPE
  24. #define    CHARTYPE    unsigned char
  25. #endif
  26. #define    MAXPAT    256
  27.  
  28. #include    "stats.h"
  29.  
  30. #ifndef    TABTYPE
  31. #define    TABTYPE    long
  32. #endif
  33. typedef TABTYPE Tab;
  34.  
  35. static struct
  36. {
  37.     int patlen;
  38.     CHARTYPE pat[MAXPAT];
  39.     int rarest;
  40. } pat;
  41.  
  42. prep(base, m)
  43.     CHARTYPE *base;
  44.     register m;
  45. {
  46.     CHARTYPE *skipc;
  47.     double mfreq;
  48.     int lf;
  49.  
  50.     pat.patlen = m;
  51.     if(m > MAXPAT)
  52.         abort();
  53.     memcpy(pat.pat, base, m);
  54.     skipc = 0;
  55.     stats.len = m;
  56.     mfreq = freq[pat.pat[pat.rarest = 0]];
  57.     for(lf = 1; lf < m; lf++)
  58.         if(freq[pat.pat[lf]] < mfreq){
  59.             pat.rarest = lf;
  60.             mfreq = freq[pat.pat[lf]];
  61.         }
  62.     skipc = &pat.pat[pat.rarest];
  63. }
  64.  
  65. exec(base, n)
  66.     CHARTYPE *base;
  67. {
  68.     int nmatch = 0;
  69.     register CHARTYPE *e, *s;
  70.     register CHARTYPE ch;
  71.     register np1;
  72.     register s_offset;
  73.     extern char *memchr();
  74.     register CHARTYPE *p, *q;
  75.     register CHARTYPE *ep;
  76.  
  77.     s = base;
  78.     e = base + n - (pat.patlen - pat.rarest);
  79.     ch = pat.pat[pat.rarest];
  80.     np1 = n+1;
  81.     base[n] = ch;
  82.     s_offset = -pat.rarest;
  83.     ep = pat.pat + pat.patlen;
  84.     while(s < e){
  85. #ifdef    STATS
  86.         while(ch != *s++)
  87.             stats.jump++;
  88.         stats.jump++;
  89.         if(--s > e)
  90.             break;
  91. #else
  92.         s = (CHARTYPE *)memchr(s, ch, np1);    /* must find it */
  93.         if(s > e)
  94.             break;
  95. #endif
  96. #ifdef    STATS
  97.         stats.slow++;
  98. #endif
  99.         for(p = pat.pat, q = s+s_offset; p < ep; ){
  100. #ifdef    STATS
  101.             stats.cmp++;
  102. #endif
  103.             if(*q++ != *p++)
  104.                 goto mismatch;
  105.         }
  106.         nmatch++;
  107.     mismatch:
  108.         s++;
  109. #ifdef    STATS
  110.         stats.step[1]++;
  111. #endif
  112.     }
  113.     return(nmatch);
  114. }
  115.